//
//  AudioListCell.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/12/20.
//

import UIKit
typealias RightDoneBlock = ()->()
class AudioListCell: UITableViewCell {
    fileprivate var nameLabel:UILabel!
    fileprivate var sizeAndDurLabel:UILabel!
    fileprivate var timeLabel:UILabel!
    fileprivate var audioImgView = UIImageView(image: UIImage(systemName: "waveform"))
    fileprivate var selectBtn:UIButton!
    var duoxuanLabel:UILabel!
    @objc var doneBlock:RightDoneBlock?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear
        let bgView = UIView()
        bgView.backgroundColor = MYColor.color(withHexString: "#25282D", alpha: 0.8)
        self.contentView.addSubview(bgView)
        bgView.layer.cornerRadius = pixw(p:15)
        bgView.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15))
            make.top.equalTo(pixw(p:10))
            make.right.equalTo(pixw(p:-15))
            make.bottom.equalTo(0)
        }
        
        audioImgView.tintColor = UIColor.white
        bgView.addSubview(audioImgView)
        audioImgView.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15))
            make.centerY.equalTo(bgView)
        }
        
        selectBtn = UIButton(type: .custom)
        selectBtn.setImage(UIImage(systemName: "square.and.arrow.up.fill"), for: .normal)
        selectBtn.tintColor = UIColor.white
        selectBtn.addTarget(self, action: #selector(selectBtnCilck), for: .touchUpInside)
        bgView.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.right.equalTo(pixw(p:-15));
            make.centerY.equalTo(bgView);
            make.width.height.equalTo(pixw(p:40));
        }
        
        duoxuanLabel = UILabel()
        duoxuanLabel.text = ""
        duoxuanLabel.layer.cornerRadius = pixw(p:10)
        duoxuanLabel.font = MY_SCALE_RegularFONT(12)
        duoxuanLabel.layer.borderColor = UIColor.white.cgColor
        duoxuanLabel.layer.borderWidth = pixw(p:0.5)
        duoxuanLabel.clipsToBounds = true
        duoxuanLabel.textAlignment = .center
        duoxuanLabel.textColor = MYColor.color(withHexString: "#FC2F55")
        bgView.addSubview(duoxuanLabel)
        duoxuanLabel.snp.makeConstraints { make in
            make.centerY.equalTo(audioImgView);
            make.width.height.equalTo(pixw(p:20));
            make.right.equalTo(bgView).offset(pixw(p:-15));
        }
        
        nameLabel = UILabel()
        nameLabel.text = "--"
        nameLabel.font = MY_SCALE_FONT(13)
        nameLabel.textColor = MYOrange
        nameLabel.lineBreakMode = .byTruncatingMiddle
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:55));
            make.top.equalTo(pixw(p:10));
            make.right.equalTo(selectBtn.snp.left).offset(pixw(p:-15));
        }
        
        
        sizeAndDurLabel = UILabel()
        sizeAndDurLabel.text = "0MB  10:10:10"
        sizeAndDurLabel.font = MY_SCALE_LightFONT(11)
        sizeAndDurLabel.textColor = MYColor.color(withHexString: "#ffffff")
        bgView.addSubview(sizeAndDurLabel)
        sizeAndDurLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nameLabel);
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p:4));
        }
        
        timeLabel = UILabel()
        timeLabel.text = "--"
        timeLabel.font = MY_SCALE_LightFONT(11)
        timeLabel.textColor = UIColor.white
        timeLabel.lineBreakMode = .byTruncatingMiddle
        bgView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.right.equalTo(nameLabel);
            make.top.equalTo(sizeAndDurLabel.snp.bottom).offset(pixw(p:4));
            make.bottom.equalTo(-pixw(p:10));
        }
        
    }
    
    
    /// 更新数据
    /// - Parameters:
    ///   - name: 名称带后缀
    ///   - docum: 路径
    ///   - type: 1 录音 2录像 3拍照
    func updata(name:String,docum:String,type:Int){
        selectBtn.isHidden = false
        duoxuanLabel.isHidden = true
        let nameArray = name.components(separatedBy: ".")
        if nameArray.count < 2 {
            selectBtn.isUserInteractionEnabled = false
            selectBtn.setImage(UIImage(named:"com_right"), for: .normal)
            audioImgView.image = UIImage(systemName: "folder")
        }else{
            selectBtn.isUserInteractionEnabled = true
            selectBtn.setImage(UIImage(systemName: "square.and.arrow.up.fill"), for: .normal)
            
            if type == 1 {
                audioImgView.image = UIImage(systemName: "waveform")
            }else if type == 2 {
                audioImgView.image = UIImage(systemName: "video")
            }else if type == 3 {
                audioImgView.image = UIImage(systemName: "livephoto")
            }
        }
        nameLabel.text = name
        let fileManager = FileManager.default
        let path = (Helper.getPath() ?? "") + "/" + docum + "/" + name
//        let pathUrl = URL(fileURLWithPath: path)
        do {
            let attributes = try fileManager.attributesOfItem(atPath:path)
            
            // 注意：这里我们使用 modificationDate 而不是创建时间，因为创建时间可能不可用
            if let modificationDate = attributes[.modificationDate] as? Date {
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                let str = formatter.string(from: modificationDate)
                timeLabel.text = "\(str)  "
            }
            
            // 从属性字典中获取文件大小
            if let fileSizeNumber = attributes[.size] as? UInt64 {
                let mSize:Float = Float(fileSizeNumber) / 1000 / 1000
                let kSize:Float = Float(fileSizeNumber) / 1000
                var sizeStr = NSString(format: "%dB",fileSizeNumber)
                if fileSizeNumber > 0 {
                    sizeStr = NSString(format: "%.0fK",kSize)
                }
                if mSize > 1 {
                    sizeStr = NSString(format: "%.2fM",mSize)
                }
                
                if nameArray.count < 2 {
                    sizeAndDurLabel.text = ""
                }else{
                    sizeAndDurLabel.text = "\(sizeStr)"
                    let time = Helper.getFileDuratiomn(path)
                    if time != "00:00:00" {
                        sizeAndDurLabel.text = "\(sizeStr)   \(time ?? "00:00:00")"
                    }
                }
            }
            
        } catch {
            // 处理错误，例如文件不存在或无法读取属性
            print("Error retrieving folder attributes")
        }
    }
    
    @objc func selectBtnCilck(){
        if((self.doneBlock) != nil){
            self.doneBlock!()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

